package pro.uccs.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import pro.uccs.constant.OpenPlatformConstant;
import pro.uccs.controller.DemoController;
import pro.uccs.controller.RequestResourceController;
import pro.uccs.service.UCCSOAuth2RequestAuthenticator;
import pro.uccs.service.Oauth2Service;

import java.util.Arrays;

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurationSupport {


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    /**
     * 加入资源请求控制类
     * @param openOauth2Service
     * @return
     */
    @Bean
    public DemoController demoController(@Qualifier("openOauth2Service")
                                                                       Oauth2Service openOauth2Service) {
        DemoController controller = new DemoController();
        controller.setOpenOauth2Service(openOauth2Service);
        return controller;
    }
    /**
     * 加入资源请求控制类
     * @param openOauth2Service
     * @return
     */
    @Bean
    public RequestResourceController requestResourceController(@Qualifier("openOauth2Service")
                                                                       Oauth2Service openOauth2Service) {
        RequestResourceController controller = new RequestResourceController();
        controller.setOpenOauth2Service(openOauth2Service);
        return controller;
    }

    /**
     * 设置开放资源服务
     *
     * @param oAuth2RestTemplate
     * @return
     */
    @Bean
    public Oauth2Service openOauth2Service(
            OAuth2RestTemplate oAuth2RestTemplate) {

        AuthorizationCodeResourceDetails authorizationCodeResourceDetails = new AuthorizationCodeResourceDetails();{
            /**
             * 设置令牌获取地址
             */
            authorizationCodeResourceDetails.setAccessTokenUri(OpenPlatformConstant.ACCESS_TOKEN_URI);
            /**
             * 设置开放平台client id
             */
            authorizationCodeResourceDetails.setClientId(OpenPlatformConstant.CLIENT_ID);
            /**
             * 设置开放平台appid
             */
            authorizationCodeResourceDetails.setId(OpenPlatformConstant.APP_ID);
            /**
             * 设置用户授权地址
             */
            authorizationCodeResourceDetails.setUserAuthorizationUri(OpenPlatformConstant.USER_AUTHORIZATION_URI);
            /**
             * 设置开放平台 client secret
             */
            authorizationCodeResourceDetails.setClientSecret(OpenPlatformConstant.CLIENT_SECRET);
            authorizationCodeResourceDetails.setScope(Arrays.asList("read", "write"));
        }
        /**
         * 将oAuth2RestTemplate 注入到开放平台服务
         */
        Oauth2Service openOauth2Service = new Oauth2Service();
        openOauth2Service.setOAuth2RestTemplate(oAuth2RestTemplate);
        openOauth2Service.setAuthorizationCodeResourceDetails(authorizationCodeResourceDetails);
        return openOauth2Service;
    }


    /**
     * 获取资源配置信息
     */
    @Configuration
    @EnableOAuth2Client
    protected static class ResourceConfiguration {
        @Bean
        public OAuth2ProtectedResourceDetails sparklr() {
            AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
            /**
             * 设置开放平台appid
             */
            details.setId(OpenPlatformConstant.APP_ID);
            /**
             * 设置开放平台client id
             */
            details.setClientId(OpenPlatformConstant.CLIENT_ID);
            /**
             * 设置开放平台 client secret
             */
            details.setClientSecret(OpenPlatformConstant.CLIENT_SECRET);
            /**
             * 设置令牌获取地址
             */
            details.setAccessTokenUri(OpenPlatformConstant.ACCESS_TOKEN_URI);
            /**
             * 设置用户授权地址
             */
            details.setUserAuthorizationUri(OpenPlatformConstant.USER_AUTHORIZATION_URI);
            details.setScope(Arrays.asList("read", "write"));
            return details;
        }

        @Bean
        public OAuth2RestTemplate sparklrRestTemplate(OAuth2ClientContext clientContext) {
            /**
             * 构建OAuth2RestTemplate
             */
            OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(sparklr(), clientContext);
            UCCSOAuth2RequestAuthenticator openOAuth2RequestAuthenticator = new UCCSOAuth2RequestAuthenticator();
            oAuth2RestTemplate.setAuthenticator(openOAuth2RequestAuthenticator);
            return oAuth2RestTemplate;
        }
    }

}
